package com.tianhui.consignortwofei.mvp.ui.activity.audit.driver;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.BankInfo;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.DriverModel;
import com.tianhui.consignortwofei.mvp.model.enty.DriverInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.bankCard.BankCardInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.PictureStrategyFactory;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.BankCardStrategy;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/3/29
 * @description
 **/
public class DriverBankCardActivity extends BaseActivity {

    @BindView(R.id.activity_driver_bank_card_nameInputItemView)
    InputItemView mNameInputItemView;
    @BindView(R.id.activity_driver_bank_card_bankNameInputItemView)
    InputItemView mBankNameInputItemView;
    @BindView(R.id.activity_driver_bank_card_cardNumberInputItemView)
    InputItemView mCardNumberInputItemView;
    @BindView(R.id.activity_driver_bank_card_commitButton)
    Button mCommitButton;

    private DriverModel mDriverModel;
    private BankCardInfo mBankCardInfo;
    private PictureStrategyFactory pictureStrategyFactory;
    private PictureContext pictureContext;

    @Override
    public int getLayoutId() {
        return R.layout.activity_driver_bank_card;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {
        setTitle("编辑银行卡");
        mDriverModel = new DriverModel();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            DriverInfo driverInfo = bundle.getParcelable(KeyValue.KEY_DRIVER_INFO);
            if (driverInfo != null) {
                requestBankCardInfo(driverInfo);
            }
        }

        pictureStrategyFactory = new PictureStrategyFactory();

    }

    private void requestBankCardInfo(DriverInfo driverInfo) {
        Map<String, String> map = new HashMap<>();
        map.put("userId", driverInfo.user_id);
        mDriverModel.bankCardInfoModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        mBankCardInfo = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), BankCardInfo.class);
                        if (mBankCardInfo != null) {
                            showBankCardInfo(mBankCardInfo);
                            mCommitButton.setVisibility(View.VISIBLE);
                        } else {
                            showToast("司机未注册银行卡信息");
                        }
                    } else {
                        showToast(commonResponse.getMsg());
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    private void showBankCardInfo(BankCardInfo bankCardInfo) {
        mNameInputItemView.setContent(bankCardInfo.bankCertName);
        mBankNameInputItemView.setContent(bankCardInfo.branchName);
        mCardNumberInputItemView.setContent(bankCardInfo.bankCardNo);
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.activity_driver_bank_card_commitButton)
    public void commit() {
        String name = mNameInputItemView.getContent();
        String bankName = mBankNameInputItemView.getContent();
        String cardNumber = mCardNumberInputItemView.getContent();
        if (TextUtils.isEmpty(name)) {
            showToast("请输入持卡人姓名");
            return;
        }
        if (TextUtils.isEmpty(bankName)) {
            showToast("请输入银行卡名称");
            return;
        }
        if (TextUtils.isEmpty(cardNumber)) {
            showToast("请输入卡号");
            return;
        }

        mBankCardInfo.bankCertName = name;
        mBankCardInfo.branchName = bankName;
        mBankCardInfo.bankCardNo = cardNumber;

        requestBankCardInfoUpdate();
    }

    private void requestBankCardInfoUpdate() {
        mDriverModel.bankCardInfoUpdateModel(this, mBankCardInfo, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                showToast(commonResponse.getMsg());
                if (commonResponse.getCode() == 20000) {
                    finish();
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    @OnClick(R.id.activity_driver_bank_card_scanImageView)
    public void scanCard() {


        BankCardStrategy bankCardStrategy = (BankCardStrategy) pictureStrategyFactory.createStrategy(BankCardStrategy.class);
        pictureContext = new PictureContext(bankCardStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<BankCardInfo>() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo<BankCardInfo> resultInfo) {
                if (resultInfo != null && resultInfo.resultInfo != null) {

                    String cardNumber = resultInfo.resultInfo.bankcardno;
                    mCardNumberInputItemView.setContent(cardNumber);
                    String bankName = BankInfo.getNameOfBank(cardNumber);
                    mBankNameInputItemView.setContent(bankName);

                }

            }

            @Override
            public void selectPhotoFailed() {
            }
        });

    }
}
